package com.vpclub.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author : leimingchao
 * @version V1.0
 * @Project: jdbc
 * @Package com.vpclub.util
 * @Description: TODO
 * @date Date : 2021年05月30日 10:47
 */
public class JDBCUtils2 {

    /**
     * 使用c3p0的数据库连接池技术
     * @return
     * @throws SQLException
     */
    /**
     * 数据库连接池只需提供一个即可
     */
    private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("helloc3p0");

    public static Connection getConnection() throws SQLException {
        Connection connection = comboPooledDataSource.getConnection();
        return connection;
    }

    /**
     * 关闭资源
     *
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void closeResource(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 使用DBCP数据库连接池技术获取数据库连接
     *
     * @return
     * @throws Exception
     */

    private static DataSource dataSource;

    static {
        try {
            Properties properties = new Properties();
            //方式一：
            InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
            properties.load(inputStream);
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection2() throws Exception {

        //方式二：
        //FileInputStream fileInputStream = new FileInputStream(new File("src/main/resources/dbcp.properties"));
        //properties.load(fileInputStream);
        Connection connection = dataSource.getConnection();
        return connection;
    }

    /**
     * 使用druid数据库连接池技术
     */
    private static DataSource datasource1;
    static {
        try {
            Properties properties = new Properties();
            InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            properties.load(inputStream);
            datasource1 = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection3() throws SQLException {
        Connection connection = datasource1.getConnection();
        return connection;
    }

    /**
     * 使用DBUtils jar包中提供的DBUtils工具类，实现资源的关闭
     * @param connection
     * @param statement
     * @param resultSet
     */
    public static void closeResource2(Connection connection, Statement statement, ResultSet resultSet) {
//        try {
//            DbUtils.close(resultSet);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        try {
//            DbUtils.close(statement);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        try {
//            DbUtils.close(connection);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        DbUtils.closeQuietly(resultSet);
        DbUtils.closeQuietly(statement);
        DbUtils.closeQuietly(connection);
    }
}
